package com.iedu.demo.lgq.service.impl;

import com.iedu.demo.lgq.entity.ChatMessage;
import com.iedu.demo.lgq.mapper.ChatMessageMapper;
import com.iedu.demo.lgq.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ChatMessageMapper chatMessageMapper;

    @Override
    public void sendMessage(ChatMessage chatMessage) {
        // 设置时间戳
        chatMessage.setTimestamp((java.sql.Date) new Date());
        // 保存消息到数据库
        chatMessageMapper.insertChatMessage(chatMessage);
    }

    @Override
    public List<ChatMessage> getChatHistory(String sender, String receiver) {
        return chatMessageMapper.findChatHistory(sender, receiver);
    }

    @Override
    public List<ChatMessage> getUnreadMessages(String receiver, Date lastTimestamp) {
        return chatMessageMapper.findUnreadMessages(receiver, lastTimestamp);
    }
}